package com.rabbitmq.demo.conf;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 发布订阅模式
 *
 * @Author yi.cheng
 * @Date 2023-7-12 15:55
 **/
@Configuration
public class PublishSubscribeConfig {
    // 队列名1
    public final static String FANOUT_QUEUE_ONE = "fanout_queue_one";
    // 队列名2
    public final static String FANOUT_QUEUE_TWO = "fanout_queue_two";
    // 交换机名
    public final static String FANOUT_EXCHANGE_NAME = "fanout_exchange";

    // fanout 广播者模式队列1
    @Bean
    public Queue fanoutQueueOne() {
        return new Queue(FANOUT_QUEUE_ONE, true);
    }

    // fanout 广播者模式队列2
    @Bean
    public Queue fanoutQueueTwo() {
        return new Queue(FANOUT_QUEUE_TWO, true);
    }

    // fanout 交换器
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE_NAME);
    }

    // 广播模式绑定队列1，2
    @Bean
    public Binding fanoutExchangeBingingOne() {
        return BindingBuilder.bind(fanoutQueueOne()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutExchangeBingingTwo() {
        return BindingBuilder.bind(fanoutQueueTwo()).to(fanoutExchange());
    }
}
